<?php
class Core_Common_Menu {
	public static function LoggedinMenu() {
			$auth = Zend_Auth::getInstance();
		if($auth->hasIdentity()) {
			//$logoutUrl = $this->view->url(array('controller'=>'auth','action'=>'logout'));
			$logoutUrl = "/default/auth/logout";
			$user = $auth->getIdentity();
			
			$username = ucfirst($user->email);
			$string = 'Logged in as '.$username.' | <a href="'.$logoutUrl.'">Log out</a>';
		}
		else {
			//$loginUrl = $this->view->url(array('controller'=>'auth','action'=>'identify'));
			$loginUrl = "/default/auth/identify";
			//$RegistrationUrl =$this->view->url(array('controller'=>'auth','action'=>'registercustomer'));
			$RegistrationUrl = "/default/auth/registercustomer";
			$string = '<a href="'.$loginUrl.'">Log in</a> | <a href="'.$RegistrationUrl.'">Register as customer</a>';
			//$RegistrationUrl =$this->view->url(array('controller'=>'auth','action'=>'registertrainer'));
			$RegistrationUrl = "/default/auth/registertrainer";
			$string =$string. ' | <a href="'.$RegistrationUrl.'">Register as Trainer</a>';
		}
		return $string;
	}
	
	public static function Menu() {
		$auth = Zend_Auth::getInstance();
		$identity = $auth->getIdentity();
		$menu = NULL;
		if($identity!==null) {
			
			$cache = new Memcache();
			$cache->connect('localhost',11211) or null;
			if(isset($cache) && $cache->get('menu')) {
				$buffer = $cache->get('menu');
				$ruleid = Core_Business_Rule::getRuleByName($identity->getRole())->Id;
				$menu = isset($buffer[$ruleid]) ? $buffer[$ruleid] : NULL;
			}
			
			if($menu === NULL)
			{
				$rootmenu = Core_Business_Rulemenu::getMenuitemLevel0(Core_Business_Rule::getRuleByName($identity->getRole())->Id);
				$Menutt = new Core_Common_Menu();
				$menu = $Menutt->assemble($rootmenu);
				if(isset($cache)) {
					$buffer = isset($buffer) ? $buffer : array();
					$buffer[Core_Business_Rule::getRuleByName($identity->getRole())->Id] = $menu;	 
					$cache->set("menu",$buffer,false,1800);
				}
			}
		}	
		return $menu;
	}
	protected function assemble($root) {
		$menu = array();
		foreach ($root as $item) {
			//if($item->Disabled==0 && Core_Business_Rulemenu::allow($item, Core_Business_Rule::getRuleByName($this->_helper->Acl->getUser()->getRole())->Id)==true) {
			if($item->Disabled==0 && Core_Business_Rulemenu::allow($item, Core_Business_Rule::getRuleByName(Core_Common_Controllers_Actions_Helpers_Acl::getUser()->getRole())->Id)==true) {
				
				$aritem = array("label"=>$item->Label,
								"order"=>$item->Order,
								"class"=>"menuitem");
				if ($item->url <> '#') {
					$aritem["module"]=$item->Module;
					$aritem["controller"]=$item->Controller;
					$aritem["action"]=$item->Action;
				}
				else {
					$aritem["uri"]="#";
				}
				$child = $item->getChild();
				$sub=array();
				if(isset($child)) {
					$nextlevel = $this->assemble($child);
					$sub=$nextlevel;
				}
				$aritem["pages"]=$sub;
				$menu[]=$aritem;
			}
		}
		//var_dump($menu);
		return $menu;
	}
}
?>